package com.vitogiovanni.tobook.utilities;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class JsonParser {

	public static final Key jsonToKey(String json)
	{
		Key key = null;
		int firstIdPosition = json.indexOf("\"id\""); 
		int typePosition = json.indexOf("\"kind\"");
		String keyType;
		String keyId;
		Key parentKey = null;
		keyId = json.substring(firstIdPosition+6, json.indexOf("\"", firstIdPosition+6));
		keyType = json.substring(typePosition+8, json.indexOf("\"", typePosition+8));
		System.out.println("Kind: " + keyType + " Id: " + keyId);
		if(json.contains("\"parent\""))
		{
			parentKey = jsonToKey(json.substring(json.indexOf("\"parent\"")+10));
		}
		
		return KeyFactory.createKey(parentKey, keyType, Long.parseLong(keyId));		
	}
}
